<?php
namespace app\api\controller;
use app\model\ImGroupUserModel;
use app\model\UserModel;
use support\Request;
use think\facade\Db;
class UserController extends Base
{




    public function pwdLogin(Request $request){
        $phone = $request->post('phone','');
        $possword = $request->post('possword','');
        $user=UserModel::where('phone',$phone)->where('password',md5( sha1($possword).'lizhili'))->field('id,name,user_no,img,sex,sheng_html')->find();
        if(!$user){
            e('找不到信息，请注册后登录！');
        }else{
            $token = md5(time().$phone);
            UserModel::where('id',$user['id'])->update([
                'token'=>$token
            ]);
            $groups=ImGroupUserModel::where('user_id',$user['id'])->column('group_id');
            $groups=array_unique($groups);
            s(['token'=>$token,'userInfo'=>$user,'groups'=>$groups]);
        }
    }
    public function registerLogin(Request $request){
        $phone = $request->post('phone','');
        $possword = $request->post('possword','');
        $user=UserModel::where('phone',$phone)->field('id,name,user_no,img,sex,sheng_html')->find();
        if($user){
            e('手机已经注册，无需重新注册');
        }else{
            $token = md5(time().$phone);
            $UserIns=UserModel::create([
                'phone'=>$phone,
                'password'=>md5( sha1($possword).'lizhili'),
                'token'=>$token
            ]);

            UserModel::where('id',$UserIns->id)->update([
                'user_no'=>generateCode(4).$UserIns->id
            ]);
            $user=UserModel::where('id',$UserIns->id)->field('id,name,user_no,img,sex,sheng_html')->find();
            s(['token'=>$token,'userInfo'=>$user,'groups'=>[]]);
        }
    }
    //微信小程序登录
    public function wxLogin(Request $request){
        $code = $request->post('code','');
        $inviterId = $request->post('inviterId',0);
        $rawData = $request->post('rawData','');
        $raw=\json_decode($rawData,true);
        $appid=config('wx.xcx_appid');
        $secret=config('wx.xcx_secret');
        $code=$this->curl('https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code');
        if($code and $raw){
            $raw['openid']=$code['openid'];
            if($user=UserModel::where('openid_wx',$code['openid'])->find()){
                $token=md5($user['openid_wx'].time());
                UserModel::where('id',$user['id'])->update(['token'=>$token,'openid_wx'=>$code['openid']]);
                if(!$user['fid'] and $inviterId){
                    UserModel::where('id',$user['id'])->update(['fid'=>$inviterId]);
                }
                $user=UserModel::where('id',$user['id'])->field('id,name,user_no,img,sex,sheng_html')->find();
                return s(['token'=>$token,'userInfo'=>$user]);
            }else{
                $raw['token']=md5($raw['openid'].time());
                $linshi=new UserModel();
                $raw['openid_wx']=$raw['openid'];
                $raw['unionid']=isset($code['unionid']) ? $code['unionid'] : '';
                $linshi->save($raw);
                if($inviterId){
                    UserModel::where('id',$linshi->id)->update(['fid'=>$inviterId]);
                }
                UserModel::where('id',$linshi->id)->update(['user_no'=>generateCode(4).$linshi->id]);
                $user=UserModel::where('id',$linshi->id)->field('id,name,user_no,img,sex,sheng_html')->find();
                return s(['token'=>$raw['token'],'userInfo'=>$user]);
            }
        }else{
            return json(['code'=>400,'message'=>'登陆失败']);
        }
    }
}
